package com.pinyougou.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pinyougou.mapper.SpecificationMapper;
import com.pinyougou.mapper.SpecificationOptionMapper;
import com.pinyougou.pojo.TbSpecification;
import com.pinyougou.pojo.TbSpecificationOption;
import com.pinyougou.sellergoods.service.SpecificationService;
import com.pinyougou.service.impl.BaseServiceImpl;
import com.pinyougou.vo.Specification;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class SpecificationServiceImpl extends BaseServiceImpl<TbSpecification> implements SpecificationService {

    @Autowired
    private SpecificationMapper specificationMapper;
    @Autowired
    private SpecificationOptionMapper specificationOptionMapper;

    @Override
    public PageInfo<TbSpecification> search(Integer pageNum, Integer pageSize, TbSpecification specification) {
        //设置分页
        PageHelper.startPage(pageNum, pageSize);
        //创建查询对象
        Example example = new Example(TbSpecification.class);

        //创建查询条件对象
        Example.Criteria criteria = example.createCriteria();

        //模糊查询
        if (StringUtils.isNotBlank(specification.getSpecName())) {
            criteria.andLike("specName", "%" + specification.getSpecName() + "%");
        }

        List<TbSpecification> list = specificationMapper.selectByExample(example);
        return new PageInfo<>(list);
    }

    @Override
    public void addspecification(Specification specification) {
        //新增规格
        add(specification.getSpecification());
        //新增规格选项
        for (TbSpecificationOption tbSpecificationOption :specification.getSpecificationOptionList() ) {
               //设置规格选项的规格id
                tbSpecificationOption.setSpecId(specification.getSpecification().getId());
        }
        //批量新增
        specificationOptionMapper.insertList(specification.getSpecificationOptionList());
    }

    @Override
    public Specification findSpecification(Long id) {
        //获取规格
        Specification specification = new Specification();
        specification.setSpecification(findOne(id));
        //根据规格id查找规格选项列表
        TbSpecificationOption specificationOption = new TbSpecificationOption();
        specificationOption.setSpecId(id);
        List<TbSpecificationOption> specificationOptionList
                = specificationOptionMapper.select(specificationOption);

        specification.setSpecificationOptionList(specificationOptionList);
        return specification;
    }

    @Override
    public void updatespecification(Specification specification) {
                //更新规格
        update(specification.getSpecification());

        //删除规格的所有选项
        TbSpecificationOption specificationOption = new TbSpecificationOption();
        specificationOption.setSpecId(specification.getSpecification().getId());
        specificationOptionMapper.delete(specificationOption);
        //新增规格选项
        for (TbSpecificationOption tbSpecificationOption :specification.getSpecificationOptionList() ) {
            //设置规格选项的规格id
            tbSpecificationOption.setSpecId(specification.getSpecification().getId());
        }
        //批量新增
        specificationOptionMapper.insertList(specification.getSpecificationOptionList());
    }

    @Override
    public void deleteSpecificationByIds(Long[] ids) {
        //批量删除
        deleteByIds(ids);
        //删除规格选项
        Example example = new Example(TbSpecificationOption.class);
        example.createCriteria().andIn("specId", Arrays.asList(ids));
        specificationOptionMapper.deleteByExample(example);
    }

    @Override
    public List<Map<String, Object>> selectOptionList() {
        return specificationMapper.selectOptionList();
    }

}
